package org.catdroid.bomba.server.servlet;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.catdroid.bomba.server.Constants;
import org.catdroid.bomba.server.GameManager;
import org.catdroid.bomba.server.bean.Players.PlayerNotFoundException;

import com.google.gson.Gson;

@SuppressWarnings("serial")
public class FinishServlet extends HttpServlet {
	
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Gson gson=new Gson();
		GameManager gm=new GameManager();
		resp.setContentType("application/json");
		resp.getWriter().println(gson.toJson(gm.getPlayers()));
	}
	
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		

		String id= req.getParameter("id");
		Gson gson=new Gson();
	
		GameManager gm=new GameManager();
		try {
			gm.getPlayers().getPlayer(id).setFinished(true);
			if (gm.isFinishReady()) {
				gm.resetGame();
			}
			resp.setContentType("application/json");
			resp.getWriter().println(gson.toJson(gm.getPlayers()));
		} catch (PlayerNotFoundException e) {
			resp.setContentType("application/json");
			resp.getWriter().println(Constants.JSON_KO);

		}
		

	}
}
